/**
 * Created by huangp on 15-1-16.
 */

/**
 * 根据Id获取联系人信息
 *
 * @param id
 */
function getContactsById(id){
    $.ajax({
        url: '/Ajax/Contacts/get',
        type: 'post',
        dataType: 'json',
        data: {id: id},
        success: function(data) {
            if (data.error) {
                alert(data.data);

            } else {
                var obj = $("#js-add-contacts").clone();
                obj.find('#js-contacts-title span').html('修改');
                var contactsInfo = data.data;
                obj.find('input[name="id"]').val(contactsInfo.Id);
                obj.find('input[name="name"]').val(contactsInfo.Name);
                if(contactsInfo.Sex == 2) {
                    obj.find('input[name="sex"][value="2"]').attr("checked",true);
                } else {
                    obj.find('input[name="sex"][value="1"]').attr("checked",true);
                }
                obj.find('input[name="address"]').val(contactsInfo.Address);
                obj.find('input[name="cellPhone"]').val(contactsInfo.CellPhone);
                obj.find('input[name="landline"]').val(contactsInfo.Landline);
                obj.find('input[name="fax"]').val(contactsInfo.Fax);
                obj.find('input[name="QQ"]').val(contactsInfo.QQ);
                obj.find('input[name="email"]').val(contactsInfo.Email);
                obj.find('input[name="office"]').val(contactsInfo.Office);
                if(contactsInfo.Status == 1) {
                    obj.find('input[name="contacts_status"][value="1"]').attr("checked",true);
                } else if(contactsInfo.Status == 2) {
                    obj.find('input[name="contacts_status"][value="2"]').attr("checked",true);
                } else {
                    obj.find('input[name="contacts_status"][value="0"]').attr("checked",true);
                }
                obj.find("#branchId option[value = "+contactsInfo.BranchId+"]").attr("selected",true);
                obj.find('input[name="remark"]').val(contactsInfo.Remark);
                obj.modal({backdrop: 'static'});
                obj.on('hide', function() {
                    $(this).remove();
                });
            }
        }
    })
}

/**
 * 根据Id删除公司联系人
 *
 * @param id
 */
function delContacts(id,companyId) {
    var r=confirm("是否删除这条数据？");
    if(r){
        $.ajax({
            url: '/Ajax/Contacts/del',
            type: 'post',
            dataType: 'json',
            data: {id:id,
                companyId:companyId
            },
            success: function(data) {
                if (data.error) {
                    alert(data.data);
                } else {
                    alert(data.data);
                    window.location.href = "/Contacts/Lists";
                }
            }
        });
    }
}

function getCertificate(id){
    $.ajax({
        url: '/Ajax/Certificate/get',
        type: 'post',
        dataType: 'json',
        data: {id: id},
        success: function(data) {
            if (data.error) {
                alert(data.data);
            } else {
                var certificateInfo = data.data;
                var obj = $('#js-add-certificate').clone();
                obj.find('#js-certificate-title span').html('修改');
                obj.find('input[name="id"]').val(certificateInfo.Id);
                obj.find('input[name="name"]').val(certificateInfo.Name);
                obj.find('input[name="releaseOrgan"]').val(certificateInfo.ReleaseOrgan);
                obj.find('input[name="acope"]').val(certificateInfo.Acope);
                if(certificateInfo.Status == 0){
                    obj.find('input[name="certificate_status"][value="2"]').attr("checked",true);
                }
                params={"page":0,"isSc":1,"isNoSc":1,"isDel":0,"isLibraries":0,"list":[{"c1":certificateInfo.Pid,"c2":certificateInfo.CategoryId,"c3":certificateInfo.LevelId,"sort":"100","isMaster":0}]};
                obj.find('#js-categoryId').category({//绑定联动
                    c2:  obj.find('#js-categoryId1'),
                    c3:  obj.find('#js-levelId'),
                    params: params
                });
                obj.modal({backdrop: 'static'});
                obj.on('hide', function() {
                    $(this).remove();
                });
            }
        }
    })
}

function showAddCertificate(){
    var obj = $('#js-add-certificate').clone();
    obj.find('#js-categoryId').category({//绑定联动
        c2:  obj.find('#js-categoryId1'),
        c3:  obj.find('#js-levelId'),
        params: params
    });
    obj.modal({backdrop: 'static'});
    obj.on('hide', function() {
        $(this).remove();
    });
}
/**
 * 添加资质
 *
 * @param obj
 *
 * @param companyId
 */
function saveCertificate(obj, companyId) {
    var id = $(obj).parent().parent().find('input[name="id"]').val();
    var name = $(obj).parent().parent().find('input[name="name"]').val();
    var releaseOrgan = $(obj).parent().parent().find('input[name="releaseOrgan"]').val();
    var acope = $(obj).parent().parent().find('input[name="acope"]').val();
    var categoryId = $(obj).parent().parent().find("#js-categoryId").val();
    var categoryId1 = $(obj).parent().parent().find("#js-categoryId1").val();
    var levelId = $(obj).parent().parent().find("#js-levelId").val();
    var status = $(obj).parent().parent().find('input[name="certificate_status"]').filter(':checked').val();
    if (!name) {
        alert('名称必须填写');
        return;
    }
    if (!categoryId) {
        alert('请选择资质类型');
        return;
    }
    if (!categoryId1) {
        alert('请选择专业');
        return;
    }
    if (!levelId) {
        alert('请选择等级');
        return;
    }
    $.ajax({
        url: '/Ajax/Certificate/save',
        type: 'post',
        dataType: 'json',
        data: {id :id,
            companyId: companyId,
            name:name,
            releaseOrgan: releaseOrgan,
            acope: acope,
            categoryId:categoryId1,
            levelId:levelId,
            status:status
        },
        success: function(data) {
            if (data.error) {
                alert(data.data);
            } else {
                alert(data.data);
                window.location.reload();
            }
        }
    });
}

/**
 * 根据Id删除公司资质
 *
 * @param id
 */
function delCertificate(id) {
    var r=confirm("是否删除这条数据？")
    if(r){
        $.ajax({
            url: '/Ajax/Certificate/del',
            type: 'post',
            dataType: 'json',
            data: {id:id},
            success: function(data) {
                if (data.error) {
                    alert(data.data);
                } else {
                    alert('资质删除成功！');
                    window.location.reload();
                }
            }
        });
    }
}

/**
 * 根据Id获取公司分支机构
 *
 * @param id
 */
function getBranchById(id){
    $.ajax({
        url: '/Ajax/Branch/get',
        type: 'post',
        dataType: 'json',
        data: {id: id},
        success: function(data) {
            if (data.error) {
                alert(data.data);
            } else {
                var branchInfo = data.data;
                var obj = $('#js-add-branch').clone();
                obj.find('#js-branch-title span').html('修改');
                obj.find('input[name="branchId"]').val(branchInfo.BranchId);
                obj.find('input[name="branchName"]').val(branchInfo.BranchName);
                obj.find('input[name="branchNumber"]').val(branchInfo.BranchNumber);
                obj.find('input[name="branchAddress"]').val(branchInfo.BranchAddress);
                obj.find('input[name="branchPrincipal"]').val(branchInfo.BranchPrincipal);
                obj.find('input[name="branchLicenseNumber"]').val(branchInfo.BranchLicenseNumber);
                obj.find('input[name="branchTaxNumber"]').val(branchInfo.BranchTaxNumber);
                obj.modal({backdrop: 'static'});
                obj.on('hide', function() {
                    $(this).remove();
                });

            }
        }
    })
}
/**
 * 添加分支机构
 *
 * @param obj
 *
 * @param companyId
 */
function saveBranch(obj, companyId) {
    var branchId = $(obj).parent().parent().find('input[name="branchId"]').val();
    var branchName = $(obj).parent().parent().find('input[name="branchName"]').val();
    var branchNumber = $(obj).parent().parent().find('input[name="branchNumber"]').val();
    var branchAddress = $(obj).parent().parent().find('input[name="branchAddress"]').val();
    var branchPrincipal = $(obj).parent().parent().find('input[name="branchPrincipal"]').val();
    var branchLicenseNumber = $(obj).parent().parent().find('input[name="branchLicenseNumber"]').val();
    var branchTaxNumber = $(obj).parent().parent().find('input[name="branchTaxNumber"]').val();
    if (!branchName) {
        alert('分支机构名称必须填写');
        return;
    }
    $.ajax({
        url: '/Ajax/Branch/save',
        type: 'post',
        dataType: 'json',
        data: {branchId: branchId,
            companyId: companyId,
            branchName: branchName,
            branchNumber: branchNumber,
            branchAddress:branchAddress,
            branchPrincipal:branchPrincipal,
            branchLicenseNumber:branchLicenseNumber,
            branchTaxNumber:branchTaxNumber
        },
        success: function(data) {
            if (data.error) {
                alert(data.data);
            } else {
                alert(data.data);
                window.location.reload();
            }
        }
    });
}

function showAddBranch(){
    var obj = $('#js-add-branch').clone();
    obj.modal({backdrop: 'static'});
    obj.on('hide', function() {
        $(this).remove();
    });
}

/**
 * 根据Id删除分支机构
 *
 * @param id
 */
function delBranch(id) {
    var r=confirm("是否删除这条数据？")
    if(r){
        $.ajax({
            url: '/Ajax/Branch/del',
            type: 'post',
            dataType: 'json',
            data: {id:id},
            success: function(data) {
                if (data.error) {
                    alert(data.data);
                } else {
                    alert('分支机构删除成功！');
                    window.location.reload();
                }
            }
        });
    }
}

function getCompany(id){
    $.ajax({
        url: '/Ajax/Company/get',
        type: 'post',
        dataType: 'json',
        data: {id: id},
        success: function(data) {
            if (data.error) {
                alert(data.data);
            } else {
                var companyInfo = JSON.parse(data.data);
                $("#js-add-Company").find('input[name="name"]').val(companyInfo.Name);
                $("#js-add-Company").find('input[name="companType"]').val(companyInfo.CompanType);
                $("#js-add-Company").find('input[name="legal"]').val(companyInfo.Legal);
                if(companyInfo.SrcStatus == '无效'){
                    $("#js-add-Company").find('input[name="srcStatus"][value="无效"]').attr("checked",true);
                }
            }
        }
    })
}

/**
 * 根据公司Id修改信息
 *
 * @param obj
 */
function updateCompany(obj,id) {
    var name = $(obj).parent().parent().find('input[name="name"]').val();
    var companType = $(obj).parent().parent().find('input[name="companType"]').val();
    var legal = $(obj).parent().parent().find('input[name="legal"]').val();
    var srcStatus = $(obj).parent().parent().find('input[name="srcStatus"]').filter(':checked').val();
    if (!name) {
        alert('名称必须填写');
        return;
    }
    $.ajax({
        url: '/Ajax/Company/update',
        type: 'post',
        dataType: 'json',
        data: {id :id,
            name:name,
            companType:companType,
            legal:legal,
            srcStatus: srcStatus
        },
        success: function(data) {
            if (data.error) {
                alert(data.data);
            } else {
                alert(data.data);
                window.location.reload();
            }
        }
    });
}

/**
 * 根据公司Id删除公司信息
 *
 * @param id
 */
function delCompany(id){
    var r = confirm("是否删除这条数据？")
    if(r){
        $.ajax({
            url: '/Ajax/Company/del',
            type: 'post',
            dataType: 'json',
            data: {id:id},
            success: function(data) {
                if (data.error) {
                    alert(data.data);
                } else {
                    alert('公司删除成功！');
                    window.location.href='/Company/Update';
                }
            }
        });
    }
}

/**
 * 根据Id删除公司联系人
 *
 * @param id
 */
function delCompanyContacts(id, companyId) {
    var r=confirm("是否删除这条数据？");
    if(r){
        $.ajax({
            url: '/Ajax/Contacts/delCompanyContacts',
            type: 'post',
            dataType: 'json',
            data: {id:id,
                companyId: companyId
            },
            success: function(data) {
                if (data.error) {
                    alert(data.data);
                } else {
                    alert(data.data);
                    window.location.href = "/Company/Details?id="+companyId+"&type=Contacts";
                }
            }
        });
    }
}